package org.ThreadDemo.sync;
//创建资源类 ，定义属性和方法


class Ticket {
    //票数
    private int number = 30;

    public synchronized void sale() {
        if(number>0){
           System.out.println(Thread.currentThread().getName()+": 卖出："+(number--) +"剩下："+number);
        }
    }
}
//
public class SaleTicket {

    public static void main(String[] args) {

        Ticket t = new Ticket();

        new Thread( new Runnable(){

            @Override
            public void run(){
                for(int i=0;i<40;i++){
                    t.sale();
                }

            }

        },"AA").start();


        new Thread( new Runnable(){

            @Override
            public void run(){
                for(int i=0;i<40;i++){
                    t.sale();
                }

            }

        },"BB").start();


        new Thread( new Runnable(){

            @Override
            public void run(){
                for(int i=0;i<40;i++){
                    t.sale();
                }

            }

        },"CC").start();


    }



}
